package com.sud.framework.system.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 功能：日期操作
 * 作者：SUD
 * 时间：2018/6/26 11:36
 */
public class DateUtil {
    public final static String FORMAT_MM = "mm";
    public final static String FORMAT_TIME = "HH:mm:ss";
    public final static String FORMAT_2 = "dd";
    public final static String FORMAT_4 = "MM月dd日";
    public final static String FORMAT_4_1 = "MM/dd";
    public final static String FORMAT_8 = "yyyyMMdd";
    public final static String FORMAT_10 = "yyyy-MM-dd";
    public final static String FORMAT_19_yMdHms = "yyyyMMddHHmmss";
    public final static String FORMAT_19 = "yyyy-MM-dd HH:mm:ss";
    public final static String FORMAT_19_0 = "yyyy-MM-dd 00:00:01";
    public final static String FORMAT_19_0_0 = "yyyy-MM-dd 00:00:00";
    public final static String FORMAT_19_H = "yyyy-MM-dd HH:00:00";
    public final static String FORMAT_19_M_00 = "yyyy-MM-dd HH:mm:00";
    public final static String FORMAT_19_M_30 = "yyyy-MM-dd HH:mm:30";
    public final static String FORMAT_19_M_59 = "yyyy-MM-dd HH:59:59";
    public final static String FORMAT_19_23_59 = "yyyy-MM-dd 23:59:59";
    public final static String FORMAT_13_H = "yyyy-MM-dd HH";
    /**
     * 按照指定格式获取当前时间
     * @param format
     * @return
     */
    public static String getCurrentTime(String format){
        String dateStr;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        dateStr = ymd.format(new Date());
        return dateStr;
    }
    /**
     * 字符串类型的时间按照指定格式转换为Date类型
     * @param time
     * @param format
     * @return
     */
    public static Date parseStrToDate(String time, String format){
        Date date = null;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        try {
            date = ymd.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 把Date类型日期按照指定格式转换成字符串
     * @param date
     * @param format
     * @return
     */
    public static String formatDateToStr(Date date,String format){
        String dateTime = null;
        SimpleDateFormat ymd = new SimpleDateFormat(format);
        try {
            dateTime = ymd.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateTime;
    }
    /**
     * 日期加减天数
     * @param smdate 原始日期
     * @param day 加减天数
     * @return
     */
    public static Date dateArithmetic(Date smdate,int day)
    {
        GregorianCalendar gc=new GregorianCalendar();
        try {
            gc.setTime(smdate);
            gc.add(5,day);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gc.getTime();
    }
    /**
     * 求两个日期相差天数
     * @param smdate 开始日期
     * @param bdate 结束日期
     * @return
     */
    public static int daysBetween(Date smdate,Date bdate)
    {
        Long between_days = null;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            smdate=sdf.parse(sdf.format(smdate));
            bdate=sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            between_days=(time2-time1)/(1000*3600*24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return between_days.intValue();
    }
}
